package com.xiaoerkeji.eshop.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;

public class ClipboardUtils {

    public static String getClipboardContent(Context context) {
        ClipboardManager clipboard = (ClipboardManager)
                context.getSystemService(Context.CLIPBOARD_SERVICE);

        if (clipboard == null || !clipboard.hasPrimaryClip())
            return "";

        ClipData clipData = clipboard.getPrimaryClip();
        if (clipData.getItemCount() == 0)
            return "";

        ClipData.Item item = clipData.getItemAt(0);
        return item.getText() != null ? item.getText().toString() : "";
    }

    public static void clearClipboard(Context context) {
        ClipboardManager clipboard = (ClipboardManager)
                context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            clipboard.clearPrimaryClip();
        } else {
            clipboard.setPrimaryClip(ClipData.newPlainText("", ""));
        }
    }

}
